/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package formula.node;

import java.util.Hashtable;

/**
 *
 * @author CJ
 */
public class OperationNode extends Node{

    protected OperationNodeType type;

    public OperationNode(Node left, Node right, OperationNodeType type, int depth){
        super(left, right, depth);
        this.type = type;
    }

    public OperationNodeType getType(){
        return this.type;
    }

    public void setLeft(Node left){
        this.leftNode = left;
    }

    public void setRight(Node right){
        this.rightNode = right;
    }

    public void setType(OperationNodeType type){
        this.type = type;
    }

    public double eval(Hashtable<String, Double> varables){
        switch(this.type){
            case PLUS:
                return this.leftNode.eval(varables) + this.rightNode.eval(varables);
            case MINUS:
                return this.leftNode.eval(varables) - this.rightNode.eval(varables);
            case MULTI:
                return this.leftNode.eval(varables) * this.rightNode.eval(varables);
            case DIVIDE:
                return this.leftNode.eval(varables) / this.rightNode.eval(varables);
            default:
                throw new IllegalArgumentException("In OperationNode::Eval, type is invalid.");
        }
    }

    public String toString(){
        return "( " + this.type.toString() + ' ' + this.leftNode.toString() + ' ' + this.rightNode.toString() + " )";
    }

    public OperationNode clone(){
        return new OperationNode(this.leftNode.clone(), this.rightNode.clone(), this.type, this.depth);
    }

    public boolean reducable(){
        //We are never reducable
        return false;
    }
}
